<!doctype html>
<html lang="en" xmlns:v-bind="http://www.w3.org/1999/xhtml" xmlns:v-on="http://www.w3.org/1999/xhtml" xmlns:v-model="http://www.w3.org/1999/xhtml" xmlns:v-demo="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        .router-link-active {
            color:#f00;
        }
        pre {
            font-family: sans-serif;
            font-size: 1em;
            line-height:1.5em;
        }

    </style>
</head>
<body>
    <div id="app"></div>
</body>

<script src="../../../vender/vue@2.4.2.js"></script>
<script src="../../../vender/vue-router@2.7.0.js"></script>

<script>
    const Home={template:`<router-view class="home-view"></router-view>`};
    const Default={template:`<div>Default</div>`};
    const Foo={template:`<div>Foo</div>`};
    const Bar={template:`<div>Bar</div>`};
    const Baz={template:`<div>Baz</div>`};
    const WithParams={template:`<div>id:{{$route.params.id}}</div>`};

    const router=new VueRouter({
        routes:[
            {
                path:'/',
                component:Home,
                children:[
                    {path:'',component:Default},
                    {path:'foo',component:Foo},
                    {path:'bar',component:Bar},
                    {path:'baz',name:'baz',component:Baz},
                    {path:'with-params/:id',component:WithParams},
                    {path:'redirect',redirect:'foo'}
                ]
            },

            {path:'/absolute-redirect',redirect:'/bar'},
            {path:'/dynamic-redirect/:id?',redirect:to=>{
                console.log(to);
                const {hash,params,query}=to;
                if(query.to=='foo')return {path:'/foo',query:null};
                if(hash=='#baz')return {name:'baz',hash:''};
                if(params.id)return '/with-params/:id';
                return '/bar';
            }},

            {path:'/named-redirect',redirect:{name:'baz'}},

            {path:'/redirect-with-params/:id',redirect:'/with-params/:id'},

            {path:'*',redirect:'/'}

        ]
    });

    const app=new Vue({
        router,
        template:`<div>
            <h1>Redirect</h1>
            <ul>
                <li><router-link to="/">/</router-link></li>
                <li><router-link to="/redirect">/redirect(to /foo)</router-link></li>
                <li><router-link to="/redirect?foo=bar">/redirect?foo=bar</router-link></li>
                <li><router-link to="/absolute-redirect">/absolute-redirect (to /bar)</router-link></li>
                <li><router-link to="/dynamic-redirect">/dynamic-redirect (to /bar)</router-link></li>
                <li><router-link to="/dynamic-redirect/123">/dynamic-redirect/123 (to /with-params/123)</router-link></li>
                <li><router-link to="/dynamic-redirect/?to=foo">/dynamic-redirect/?to=foo (to /foo)</router-link></li>
                <li><router-link to="/dynamic-redirect/#baz">/dynamic-redirect/#baz (to /baz)</router-link></li>
                <li><router-link to="/named-redirect">/name-redirect (to /baz)</router-link></li>
                <li><router-link to="/redirect-with-params/123">/redirect-with-params/123 (to /redirect-with-params/123)</router-link></li>
                <li><router-link to="/not-found">/not-found (to /)</router-link></li>
            </ul>
            <router-view class="router-view"></router-view>
        </div>`
    }).$mount('#app');

</script>
</html>